package com.dj.xbirdie.adapter

import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.RecyclerView
import com.dj.xbirdie.R
import java.util.*

class SubjectListAdapter(
    var mContext:Context,
    var list: MutableList<MutableMap<String, Any>>
) : RecyclerView.Adapter<SubjectListAdapter.MyViewHolder>(){
    class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view)


    override fun onCreateViewHolder(parent: ViewGroup,
                                    viewType: Int): SubjectListAdapter.MyViewHolder {
        println("创建")
        val textView = LayoutInflater.from(parent.context).inflate(R.layout.subject_topic_list, parent, false);
        return MyViewHolder(textView)
    }


    val item = listOf<String>("16697a", "489fb5", "82c0cc","ede7e3","ffa62b");
    // Replace the contents of a view (invoked by the layout manager)
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        println("渲染")
        holder.view.findViewById<TextView>(R.id.subjectName).text = list[position].get("name") as String;
//        holder.view.findViewById<LinearLayout>(R.id.lable).setBackgroundColor(list[position].get("color") as Int);

        holder.view.findViewById<TextView>(R.id.subjectName).setOnLongClickListener{ v->
            run {
                val a = AlertDialog.Builder(mContext);
                a.setTitle("SELECT COLOR")
                a.setItems(item.toTypedArray(), DialogInterface.OnClickListener { dialog, which ->
                    list[position].set("color",mContext.resources.getColor(R.color.colorSuccess))
                    this.notifyDataSetChanged();
                });
                val a1 = a.create();
                a1.show();
                true;
            }
        }

        holder.view.findViewById<TextView>(R.id.subjectName).setOnClickListener { v->
            run {

            }
        }
        holder.view.findViewById<TextView>(R.id.unSubject).setOnClickListener { v->
            run {
//                unSubject(list.get(position).get("name"))
                list.removeAt(position);
                this.notifyDataSetChanged();
            }
        }

    }

    override fun getItemCount() = list.size
//    override fun subject(topic: String?) {
//        ctx.subject(topic)
//    }
//
//    override fun unSubject(topic: String?) {
//        ctx.unSubject(topic);
//    }
//
//    fun onTopicEventInterface(ctx: onTopicEventInterface) {
//        this.ctx=ctx;
//    }


}